package com.clippick;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import co.kr.clippick.R;

import com.clippick.common.GlobalValue;
import com.clippick.walkthrough.ImagePageAdapter;

public class WalkThroughActivity extends Activity {

	// common
	private Context mContext;

	// view
	private ViewPager pager;

	// walk through
	private ImageView image1, image2, image3, image4, image5;
	private int[] imageIds = { R.id.through1, R.id.through2, R.id.through3,
			R.id.through4, R.id.through5 };
	private ImageView[] indicators = { image1, image2, image3, image4, image5, };

	// data:info
	private SharedPreferences pref;
	private SharedPreferences.Editor editor;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.walkthrough);
		mContext = (Context) this;
		pref = mContext.getSharedPreferences(GlobalValue.KEY,
				Context.MODE_PRIVATE);
		editor = mContext.getSharedPreferences(GlobalValue.KEY,
				Context.MODE_PRIVATE).edit();

		editor.putBoolean("walk", true);
		editor.commit();

		// capture view id
		pager = (ViewPager) findViewById(R.id.pager);
		pager.setAdapter(new ImagePageAdapter(mContext,
				getWalkThroughDrawable()));
		pager.setOnPageChangeListener(new PageIndicatorChangeListener());

		// this.getUpDownGesture();
	}

	// to finish walkthrough activity
	private void getUpDownGesture() {
		pager.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				float upY = 0;
				float downY = 0;

				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					downY = event.getY();
					break;

				case MotionEvent.ACTION_UP:
					upY = event.getY();
					float slide1 = downY - upY;
					float slide2 = upY - downY;

					if (slide1 > 50 || slide2 > 50 || slide1 < 50
							|| slide2 < 50) {
						// finishActivityWithTransition();
					}

					break;
				}

				return true;
			}
		});
	}

	private void finishActivityWithTransition() {
		super.finish();
		finish();
		overridePendingTransition(R.anim.top_to_bottom, R.anim.bottom_to_top);
	}

	private List<Drawable> getWalkThroughDrawable() {
		List<Drawable> list = new ArrayList<Drawable>();
		list.add(getResources().getDrawable(R.drawable.walk_image1));
		list.add(getResources().getDrawable(R.drawable.walk_image2));
		list.add(getResources().getDrawable(R.drawable.walk_image3));
		list.add(getResources().getDrawable(R.drawable.walk_image4));
		list.add(getResources().getDrawable(R.drawable.walk_image5));

		this.createIndicator(list.size());

		return list;
	}

	private void createIndicator(int size) {
		for (int i = 0; i < size; i++) {
			this.indicators[i] = (ImageView) findViewById(this.imageIds[i]);
		}

		indicators[0].setImageResource(R.drawable.indicator_high);
	}

	// return when pager changes its view
	private class PageIndicatorChangeListener extends
			ViewPager.SimpleOnPageChangeListener {
		@Override
		public void onPageSelected(int position) {
			// set image by position
			for (int i = 0; i < indicators.length; i++) {
				if (i == position) {
					indicators[i].setImageResource(R.drawable.indicator_high);
				} else {
					indicators[i].setImageResource(R.drawable.indicator_low);
				}

				if (i == indicators.length - 1) {
					
				}
			}
		}
	}

	public void backPressed() {
		this.finishActivityWithTransition();
		super.onBackPressed();
	}
}
